package dbclass;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import classes.DbClass;

import beans.Autor;
import beans.Noticia;

public class AutorJDBC implements AutorDAO{	

	private Connection con;
	
	
	public Autor insert(Autor autor) throws SQLException, ClassNotFoundException
	{
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		boolean check =  stm.execute("INSERT INTO autor (id, nome, login,senha) VALUES (" +
				"NULL, '" + autor.getNome() + "', '" + autor.getLogin() + "', '" + autor.getSenha() + "')");
		
		
		if(check)
		{
			return autor;
		}	
		return null;	
	}
	
	public Autor update(Autor autor) throws SQLException, ClassNotFoundException
	{
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		boolean check = stm.execute("UPDATE autor SET nome = '" + autor.getNome() + "', login = '" + autor.getLogin() +
				"', senha = '" + autor.getSenha() + "' WHERE id = '" + autor.getId() + "'");
		
		if(check)
		{
			return autor;
		}	
		return null;
	}

	public boolean delete(long id) throws ClassNotFoundException, SQLException {
		boolean check = false;
		List<Noticia> noticias = findAllNoticias(id);
		if(noticias.isEmpty())
		{
			
			con = DbClass.getConnection();
			Statement stm = con.createStatement();
			int rows = stm.executeUpdate("DELETE FROM autor WHERE id = '" + id + "' LIMIT 1");
			if(rows > 0)
				check = true;
		}
		return check;
	}
	/* Lista de todos os autores */
	public List<Autor> findAll() throws SQLException, ClassNotFoundException {
		List<Autor> autores = new ArrayList<Autor>();
		
		con = DbClass.getConnection(); 
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM autor");
		
		
		while (result.next()) { 
			Autor autor = new Autor(result.getLong(1), result.getString(3), result.getString(4), result.getString(2));
            autores.add(autor);
        }		
		return autores;
	}

	/* Retorna um autor  */
	public Autor findById(long id) throws SQLException, ClassNotFoundException {
		Autor autor = null;
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM autor WHERE id = '" + id + "'");
		
		while (result.next()) {
			autor = new Autor(result.getLong(1), result.getString(3), result.getString(4), result.getString(2));
        }		
		return autor;
	}
	
	/* Utilizado para fazer login no sistema */
	public Autor login(String login, String senha) throws SQLException, ClassNotFoundException {
		Autor autor = null;
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM autor WHERE login = '" + login + "' and senha = '"+senha+"' ");
		
		while (result.next()) {
			autor = new Autor(result.getLong("id"), result.getString("login"), result.getString("senha"), result.getString("nome"));
        }		
		
		return autor; 
	}

	@Override
	public List<Noticia> findAllNoticias(long id) throws SQLException,
			ClassNotFoundException {
		
		List<Noticia> noticias = new ArrayList<Noticia>();
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM noticia WHERE id_autor = '" + id + "'");
		
		while (result.next()) {
			Noticia noticia = new Noticia(result.getLong(1), result.getString(2), result.getString(3), result.getString(4), 
					result.getLong(5), result.getLong(6));
			noticias.add(noticia); 	
        }		
		return noticias;	
	}

}